// ==UserScript==
// @name            知学云在线视频学习增强
// @description     知学云在线视频学习增强，课程结束通知提醒（允许zhixueyun.com通知）   
//                  参考： https://greasyfork.org/scripts/428336-%E7%9F%A5%E5%AD%A6%E4%BA%91/code/%E7%9F%A5%E5%AD%A6%E4%BA%91.user.js
// @updateURL       https://gitee.com/WOAICHIYUZHOU/static-file/raw/master/zxyPluse.js
// @downloadURL     https://gitee.com/WOAICHIYUZHOU/static-file/raw/master/zxyPluse.js
// @author          xuan
// @created         2022-02-14
// @lastUpdated     2022-02-17
// @version         1.0

// @match           https://*.zhixueyun.com/
// @require         https://cdn.jsdelivr.net/npm/jquery@3.2.0/dist/jquery.min.js

// @connect         cdn.jsdelivr.net

// @grant           unsafeWindow
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_addStyle
// @grant           GM_deleteValue
// @grant           GM_openInTab
// @run-at          document-start
// ==/UserScript==

(function () {
  'use strict';
  init();
})();

// 全局属性
const config = { childList: true }
var myInterval
var enableNotify = false

function init() {
  setTimeout(function () {
    const content = $('.content.drizzle-content')
    const current = content.data('current')
    switch (current) {
      case 'study/course/detail':
        courseDetail(content)
        break;
      case 'home/default':
        homeDefault()
        break;
      default:
        break;
    }
  }, 5000)
}

// 主页
function homeDefault() {
  let header = $('.header-content .nav .nav-list').find('a'), target = ''
  header.each((index, item) => {
    // console.log($(item).data('menu'));
    if ($(item).data('menu') == 'study/course/index') {
      target = item
    }
  })
  console.log($(target).parent());
  $(target).parent().click()
}

//课程播放页面
function courseDetail(node) {
  if (node) {
    let pageAlert = $(node).find('.course-detail-page > .page-alert')
    if (pageAlert.data('region') === 'alert-mousestay') {
      const observer = new MutationObserver(function (mutationsList) {
        for (let muation of mutationsList) {
          let target = muation.target
          let btn = $(target).find('.btn-ok')
          console.log(btn);
          //�رղ�Ҫ�߿�����ʾ����
          btn.click()
        }
      })
      observer.observe(pageAlert[0], config)
    }
    //setTimeout(submitEvaluation(), 5000)
    checkNotify()
    myInterval = setInterval(autoNextChapter, 5000)
  }
}

//章节完成自动切换未完成章节 （定时任务）
function autoNextChapter(){
  let video = document.querySelector('video');
  video.volume = 0
  video.play()
  let allCompleted = false
  if($('.anew').find('.anew-text').text().indexOf('已完成')!=-1){ allCompleted = true}
  let status = $(".chapter-list-box.required.focus").find(".pointer > span").text()
  if(status.indexOf("重新学习") != -1){
      if(allCompleted){
          clearInterval(myInterval)
          showCompletedNotification("课程已完成")
      }
      $("dl").each(function(){
          let isCompleted = $(this).find(".pointer > span").text()
          if(isCompleted.indexOf("重新学习") == -1){
              $(this).click()
              return false
          }
      });     
  }
}

// 自动提交评论
function submitEvaluation() {
  //设置评论
  $('.comment-area > .ke-container > .ke-edit > .ke-edit-iframe').contents().find('.ke-content').text("学习")
  //提交评论
  $('.comment-btn > div').click()
}

function checkNotify(){
  if("Notification" in window){
      if(Notification.permission === "granted"){
          enableNotify = true
      }else if(Notification.permission !== 'denied'){
          Notification.requestPermission(function (perm) {
              if (!('permission' in Notification)) {
                  Notification.permission = perm;
              }
              if (perm === "granted") {
                  // 创建 Notification
                  enableNotify = true
              }else{
                  console.log("not allow notification")
              }
          });
      }
  }else{
      console.log("not support notification")
  }
}

//显示桌面通知
function showCompletedNotification(val){
  var notification = new Notification($("title").html(),{
      body:val,
      icon:$('link[rel="shortcut icon"]')[0].href
  });
  document.title = val
  setTimeout(function(){
      notification.close()
  },10000)
}